package cn.edu.hznu.ocw.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import cn.edu.hznu.ocw.domain.CourseType;
import cn.edu.hznu.ocw.domain.SubType;
import cn.edu.hznu.ocw.jdbc.DBUtils;

public class CourseTypeDAO {
	private List<CourseType> courseTypes;
 
	public List<CourseType> getCourseTypes() {
		try {
			String sql = "SELECT * FROM coursetype ORDER BY cname ASC";
			Connection conn = DBUtils.getConnection();
			Statement stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery(sql);
			List<CourseType> list = new ArrayList<CourseType>();
			while (rs.next()) {
				CourseType ct = new CourseType();
				ct.setId(rs.getInt("id"));
				ct.setCname(rs.getString("cname"));
				ct.setCdesc(rs.getString("cdesc"));
				Set<SubType> subtypes = new SubTypeDAO().getByFtype(ct.getId());
				ct.setSubtypes(subtypes);
				list.add(ct);
			}
			DBUtils.close(rs, stmt, conn);
			return list;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
}
